package tools

import (
	"encoding/json"
	"fmt"
	"io"
	"k8s-platform/config"
	"net/http"
	"strings"
)

type Image struct {
	Tags []struct {
		Name string `json:"name"`
	} `json:"tags"`
}

type Project struct {
	Name string   `json:"name"`
	Tags []string `json:"tags"`
}

var jenkinsConfigTest = &config.JenkinsConfigTest{}
var Harbor_config, err = jenkinsConfigTest.ConfigTest()

func Harbor_projects() (projectNamesList []string, err error) {

	url := Harbor_config.Harbor_url + "/api/v2.0/projects"
	//url := "http://192.168.43.198:34498/api/v2.0/projects"

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("Error creating request: %v\n", err)
		return nil, err
	}

	req.SetBasicAuth(Harbor_config.Harbor_user, Harbor_config.Harbor_pass) // 设置基本认证

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("Error sending request: %v\n", err)
		return nil, err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("Error reading response body: %v\n", err)
		return nil, err
	}

	var projects []Project
	json.Unmarshal(body, &projects)

	var projectNames []string
	for _, project := range projects {
		fmt.Println(project.Name)
		projectNames = append(projectNames, project.Name)
	}
	return projectNames, err

}

func Harbor_prepositories(ProjectName string) (PrepositorieNamesList []Project, err error) {
	url := Harbor_config.Harbor_url + "/api/v2.0/projects/" + ProjectName + "/repositories"

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("Error creating request: %v\n", err)
		return
	}

	req.SetBasicAuth(Harbor_config.Harbor_user, Harbor_config.Harbor_pass) // 设置基本认证

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("Error sending request: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("Error reading response body: %v\n", err)
		return
	}

	var Prepositories []Project
	json.Unmarshal(body, &Prepositories)

	//var PrepositorieNames []string
	//for _, Prepositorie := range Prepositories {
	//	fmt.Println(Prepositorie.Name)
	//	PrepositorieNames = append(PrepositorieNames, Prepositorie.Name)
	//}

	var Projects []Project
	for _, Prepository := range Prepositories { //获取项目内镜像名称

		Prepository_str := Prepository.Name

		parts := strings.Split(Prepository_str, "/")
		if len(parts) > 1 {
			Prepository.Name = parts[1]
		}
		// sg/redis(Prepository_str) := sg(Prepository.Name)

		//获取项目内镜像版本列表
		url := Harbor_config.Harbor_url + "/api/v2.0/projects/" + ProjectName + "/repositories/" + Prepository.Name + "/artifacts/"

		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			fmt.Printf("Error creating request: %v\n", err)
			return nil, err
		}

		req.SetBasicAuth(Harbor_config.Harbor_user, Harbor_config.Harbor_pass) // 设置基本认证

		client := &http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			fmt.Printf("Error sending request: %v\n", err)
			return nil, err
		}
		defer resp.Body.Close()

		body, err := io.ReadAll(resp.Body)
		if err != nil {
			fmt.Printf("Error reading response body: %v\n", err)
			return nil, err
		}

		//获取执行版本信息
		var images []Image
		json.Unmarshal(body, &images)

		var tagNames []string
		for _, image := range images {
			for _, tag := range image.Tags {
				tagNames = append(tagNames, tag.Name)
			}
		}

		Projects = append(Projects, Project{Name: Prepository_str, Tags: tagNames})
	}

	return Projects, err
}

//      options: [
//        { name: 'redis', versions: ['1.0', '1.1', '1.2'] },
//        { name: 'mysql', versions: ['1.0', '1.1', '1.2'] },
//      ],

func Harbor_images_list(ProjectName string, ImagesName string) (VersionList []string, err error) {
	url := Harbor_config.Harbor_url + "/api/v2.0/projects/" + ProjectName + "/repositories/" + ImagesName + "/artifacts/"
	//url := "http://192.168.43.198:34498/api/v2.0/projects/sg/repositories/redis/artifacts/"

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("Error creating request: %v\n", err)
		return
	}

	req.SetBasicAuth(Harbor_config.Harbor_user, Harbor_config.Harbor_pass) // 设置基本认证

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("Error sending request: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("Error reading response body: %v\n", err)
		return
	}

	//获取执行版本信息
	var images []Image
	json.Unmarshal(body, &images)

	var tagNames []string
	for _, image := range images {
		for _, tag := range image.Tags {
			tagNames = append(tagNames, tag.Name)
		}
	}

	return tagNames, err
}
